package com.xps.and.yuntong.Utils;

import android.content.Context;
import android.media.ExifInterface;
import android.util.Log;

import com.xps.and.yuntong.App;
//import com.xps.and.driverside.App;
//import com.xps.and.driverside.R;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 文件工具类
 *
 */
public class FileUtil {
	private static Context context;
	public static String TAG = "com.xps.util.FileUtil";
	/**
	 * @return  创建缓存目录
	 */
	public static File getcacheDirectory() {
		File file = new File(App.getInstance().getApplicationContext().getExternalCacheDir(), "司机端");
		if(!file.exists()){
			boolean b = file.mkdirs();
			Log.e(TAG, "文件不存在  创建文件    "+b);
		}else{
			Log.e(TAG, "文件存在"+file.toString());
		}
		return file;
	}

	private static final int TYPE_IMAGE = 1;

	/**
	 * {@link #TYPE_IMAGE}<br/>
	 *
	 * @param type
	 * @return
	 */
	private static String getPublicFilePath(int type) {
		String fileDir = null;
		String fileSuffix = null;
		switch (type) {
			case TYPE_IMAGE:
				fileDir = App.getInstance().mPicturesDir;
				fileSuffix = ".jpg";
				break;
		}
		if (fileDir == null) {
			return null;
		}
		File file = new File(fileDir);
		if (!file.exists()) {
			if (!file.mkdirs()) {
				return null;
			}
		}
		return fileDir + File.separator + UUID.randomUUID().toString().replaceAll("-", "") + fileSuffix;
	}

	public static String getRandomImageFilePath() {
		return getPublicFilePath(TYPE_IMAGE);
	}

	/**
	 * 删除文件夹里面的所有文件
	 *
	 * @param path
	 *            String 文件夹路径 如 /sdcard/data/
	 */
	public static void delAllFile(String path) {
		File file = new File(path);
		if (!file.exists()) {
			return;
		}
		if (!file.isDirectory()) {
			return;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			System.out.println(path + tempList[i]);
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]); // 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]); // 再删除空文件夹
			}
		}
	}

	/**
	 * 删除文件夹
	 *
	 *            String 文件夹路径及名称 如/sdcard/data/
	 *            String
	 * @return boolean
	 */
	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			File myFilePath = new File(filePath);
			myFilePath.delete(); // 删除空文件夹
		} catch (Exception e) {
			System.out.println("删除文件夹操作出错");
			e.printStackTrace();
		}
	}

	/**
	 * @param path 图片路径
	 * @return  图片旋转角度
	 * */
	public static int getBitmapDegree(String path) {
		int degree = 0;
		try {
			// 从指定路径下读取图片，并获取其EXIF信息
			ExifInterface exifInterface = new ExifInterface(path);
			// 获取图片的旋转信息
			int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
					ExifInterface.ORIENTATION_NORMAL);
			switch (orientation) {
				case ExifInterface.ORIENTATION_ROTATE_90:
					degree = 90;
					break;
				case ExifInterface.ORIENTATION_ROTATE_180:
					degree = 180;
					break;
				case ExifInterface.ORIENTATION_ROTATE_270:
					degree = 270;
					break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return degree;
	}

}
